// pages/draw/index.js
const app = getApp();
const API = require('../../common/constant');
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    awardCount: 0,
    isDraw: false
  },
  getAwardNum() {
    util.http({
      url: API.URL.AWARD.FIND_AWARD,
      success: res => {
        console.log('res', res)
        let bussData = res.data.bussData
        if (bussData.awardCount > 0) {
          this.setData({
            isDraw: true,
            awardCount: bussData.awardCount
          })
        }
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },
  getAward() {
    let itemNum = 0;
    let count = 0;
    let maxCount = Math.floor(Math.random() * 9) + 18;
    let timer = setInterval(()=> {
      console.log(count, maxCount, itemNum)
      this.setData({
        itemNum: itemNum
      })
      itemNum = itemNum + 1;
      if (count == maxCount) {
        clearInterval(timer)
        this.setData({
          finish: true
        })
      }
      if(itemNum > 8){
        itemNum = 0
      }
      count++
    }, 300)
    util.httpbody({
      url: API.URL.AWARD.SAVE_AWARD,
      success: res => {
        console.log('res', res)
        this.setData({
          awardCount: this.data.awardCount - 1
        })
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAwardNum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})